﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace LIVITECH_ModbusDevLib
{
    public class OpticalDelayDev
    {
        private Thread thread = null;
        private ODLDeviceProcess devProcess = new ODLDeviceProcess();

        public OpticalDelayDev(string portname = "COM2", int baudrate = 19200, int slaveID = 2)
        {
            devProcess.portname = portname;
            devProcess.baudrate = baudrate;
            devProcess.slaveID = slaveID;
        }

        ~OpticalDelayDev()
        {
            CloseDevice();
            devProcess = null;
        }


        //打开设备
        public int OpenDevice(out string result)
        {
            result = "";
            if (thread != null) return 0;

            thread = new Thread(devProcess.ScanDevRun);
            thread.Start();
            Thread.Sleep(3000);

            if (!thread.IsAlive)
            {
                result = "打开设备串口失败";
                return -1;
            }

            return 0;
        }

        //关闭设备
        public void CloseDevice()
        {
            if (thread == null || devProcess == null) return;

            stop();
            Thread.Sleep(200);

            devProcess.reqQuitRun = true;
            int i = 10;

            while (thread.IsAlive)
            {
                Thread.Sleep(200);
                i--;
                if (i <= 0)
                {
                    thread.Abort();
                    break;
                }
            }

            thread = null;
            devProcess.reqQuitRun = false;
        }

        //返回设备是否通信有效
        public bool IsDeviceValid
        {
            get
            {
                return !devProcess.IsCommFailed;
            }
        }

        //光学延时线电机状态
        public string statusInfo
        {
            get
            {
                return devProcess.motor1StatusInfo;
            }
        }

        //光学延时线电机错误信息
        public string faultInfo
        {
            get
            {
                return devProcess.motor1FaultInfo;
            }
        }


        //返回光学延时线脉冲位置值
        public int positionPulse
        {
            get
            {
                return devProcess.motor1Pulse;
            }
        }

        //返回光学延时线编码器位置值
        public int positionEncoder
        {
            get
            {
                return devProcess.motor1Encoder;
            }
        }

        //停止光学延时线电机
        public void stop()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.ClearErrCnt();
                devProcess.cmdMotor1JogN = false;
                devProcess.cmdMotor1JogP = false;
                devProcess.cmdMotor1Home = false;
                devProcess.cmdMotor1EncAbsGo = false;
                devProcess.cmdMotor1Stop = true;    
            }
        }

        //光学延时线电机寻参回零
        public void home()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.cmdMotor1Home = true;
            }
        }

        //光学延时线电机高精度定位
        public void encAbsGo(int dest)
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.encAbsGoDestination = dest;
                devProcess.cmdMotor1EncAbsGo = true;
            }
        }

        //设置点动最短持续时间
        public UInt16 jogTimeLimit
        {
            set
            {
                devProcess.motor1JogTlimit = value;
            }
        }

        //光学延时线电机点动
        public void jog(bool isNeg)
        {
            if (isNeg)
            {
                jogN();
            }else
            {
                jogP();
            }
        }

        //光学延时线电机正向点动
        public void jogP()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.ClearErrCnt();
                devProcess.cmdMotor1JogP = true;
            }
        }

        //光学延时线电机负向点动
        public void jogN()
        {
            if (thread != null && thread.IsAlive)
            {
                devProcess.ClearErrCnt();
                devProcess.cmdMotor1JogN = true;
            }
        }


    }
}
